package org.example.sdpback.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.sdpback.domain.entity.TestCase;
import org.example.sdpback.domain.vo.TestCaseConfigDataVO;
import org.example.sdpback.sdk.response.SdpResponseData;
import org.example.sdpback.service.TestCaseService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ProjectName: sdp-back
 * @Package: org.example.sdpback.controller
 * @ClassName: TestCaseController
 * @Author: zhuxingyue
 * @Description: 用例接口
 * @Date: 2025/7/24 13:30
 * @Version: 1.0
 **/
@Tag(name = "用例管理模块")
@RestController
@RequestMapping("/testCase")
@RequiredArgsConstructor
public class TestCaseController {
    private final TestCaseService testCaseService;

    @Operation(summary = "添加用例")
    @PostMapping("/add")
    public SdpResponseData<String> addTestCase(@RequestBody TestCase testCase){
        testCaseService.saveTestCase(testCase);
        return SdpResponseData.success();
    }

    @Operation(summary = "获取用例列表")
    @GetMapping("/list")
    @Parameters({
            @Parameter(name = "createUser", description = "创建人", schema = @Schema(type = "string")),
            @Parameter(name = "caseName", description = "用例名称", schema = @Schema(type = "string"))
    })
    public SdpResponseData<Object> getTestCaseList(@RequestParam(required = false) String createUser,
                                                   @RequestParam(required = false) String caseName){

        if (!StringUtils.hasText(caseName) && !StringUtils.hasText(createUser)){
            return SdpResponseData.success(testCaseService.getTestCaseList());
        }
        return SdpResponseData.success(testCaseService.getTestCaseList(createUser, caseName));
    }

    @Operation(summary = "查询详情")
    @GetMapping("/detail/{id}")
    public SdpResponseData<Object> getTestCaseDetail(@PathVariable Long id){
        return SdpResponseData.success(testCaseService.getTestCaseById(id));
    }

    @Operation(summary = "更新用例")
    @PostMapping("/update")
    public SdpResponseData<String> updateTestCase(@RequestBody TestCase testCase){
        testCaseService.updateTestCase(testCase);
        return SdpResponseData.success();
    }

    @Operation(summary = "删除用例")
    @DeleteMapping("/delete/{id}")
    public SdpResponseData<String> deleteTestCase(@PathVariable Long id){
        testCaseService.removeById(id);
        return SdpResponseData.success();
    }

    @Operation(summary = "获取用例状态，类型数据")
    @GetMapping("/config/data")
    public SdpResponseData<TestCaseConfigDataVO> getConfigData(){
        // 获取用例状态，类型数据
        return SdpResponseData.success( testCaseService.getConfigData());
    }
}
